// Common types for the application

export interface User {
  id: string;
  name: string;
  email: string;
  avatar?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface AppConfig {
  id?: string;
  theme: 'light' | 'dark' | 'system';
  language: string;
  autoStart: boolean;
  minimizeToTray: boolean;
  notifications: boolean;
  shortcuts: {
    toggleWindow: string;
    newItem: string;
    search: string;
  };
  createdAt?: string;
  updatedAt?: string;
}

export interface WindowState {
  isVisible: boolean;
  isMaximized: boolean;
  isMinimized: boolean;
  isFocused: boolean;
}

export interface NotificationOptions {
  title: string;
  body: string;
  icon?: string;
  silent?: boolean;
  tag?: string;
}

export interface FileInfo {
  name: string;
  path: string;
  size: number;
  modified: Date;
  isDirectory: boolean;
}

export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  error?: string;
  message?: string;
}

export interface PaginationParams {
  page: number;
  limit: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

// Tauri-specific types
export interface TauriCommand<T = any> {
  command: string;
  payload?: any;
  response?: T;
}

export interface TauriEvent<T = any> {
  event: string;
  payload: T;
  windowLabel: string;
  id: number;
}

// Error types
export interface AppError {
  code: string;
  message: string;
  details?: any;
  timestamp: Date;
}

export type ErrorSeverity = 'info' | 'warning' | 'error' | 'critical';

export interface ErrorLog extends AppError {
  severity: ErrorSeverity;
  stack?: string;
  userAgent?: string;
  url?: string;
}

// Theme types
export type ThemeMode = 'light' | 'dark';

export interface ThemeConfig {
  mode: ThemeMode;
  primaryColor: string;
  accentColor: string;
  borderRadius: number;
  fontSize: number;
}

// Component props types
export interface BaseComponentProps {
  className?: string;
  children?: React.ReactNode;
  id?: string;
  'data-testid'?: string;
}

export interface ButtonProps extends BaseComponentProps {
  variant?: 'primary' | 'secondary' | 'outline' | 'ghost' | 'danger';
  size?: 'sm' | 'md' | 'lg';
  disabled?: boolean;
  loading?: boolean;
  onClick?: () => void;
  type?: 'button' | 'submit' | 'reset';
}

export interface InputProps extends BaseComponentProps {
  type?: 'text' | 'email' | 'password' | 'number' | 'search';
  placeholder?: string;
  value?: string;
  defaultValue?: string;
  onChange?: (value: string) => void;
  onBlur?: () => void;
  onFocus?: () => void;
  disabled?: boolean;
  required?: boolean;
  error?: string;
  label?: string;
  helperText?: string;
}

export interface ModalProps extends BaseComponentProps {
  isOpen: boolean;
  onClose: () => void;
  title?: string;
  size?: 'sm' | 'md' | 'lg' | 'xl';
  closeOnOverlayClick?: boolean;
  closeOnEscape?: boolean;
}

// Store types
export interface StoreState {
  loading: boolean;
  error: string | null;
  lastUpdated: Date | null;
}

export interface AppStore extends StoreState {
  config: AppConfig;
  windowState: WindowState;
  user: User | null;
  isAuthenticated: boolean;
}

// Utility types
export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export type RequiredFields<T, K extends keyof T> = T & Required<Pick<T, K>>;

export type Nullable<T> = T | null;

export type ValueOf<T> = T[keyof T];

export type KeysOfType<T, U> = {
  [K in keyof T]: T[K] extends U ? K : never;
}[keyof T];

// Event handler types
export type EventHandler<T = Event> = (event: T) => void;
export type ChangeHandler<T = string> = (value: T) => void;
export type ClickHandler = EventHandler<React.MouseEvent>;
export type KeyboardHandler = EventHandler<React.KeyboardEvent>;
export type FormHandler = EventHandler<React.FormEvent>;

// Async operation types
export type AsyncOperation<T = void> = () => Promise<T>;
export type AsyncOperationWithParams<P, T = void> = (params: P) => Promise<T>;

// Status types
export type LoadingState = 'idle' | 'loading' | 'success' | 'error';
export type ConnectionStatus =
  | 'connected'
  | 'disconnected'
  | 'connecting'
  | 'error';

// Route types
export interface RouteConfig {
  path: string;
  component: React.ComponentType;
  exact?: boolean;
  protected?: boolean;
  title?: string;
  icon?: React.ComponentType;
}

export interface NavigationItem {
  id: string;
  label: string;
  path: string;
  icon?: React.ComponentType;
  badge?: string | number;
  children?: NavigationItem[];
}

// Search types
export interface SearchOptions {
  query: string;
  filters?: Record<string, any>;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
  limit?: number;
  offset?: number;
}

export interface SearchResult<T> {
  items: T[];
  total: number;
  query: string;
  took: number;
}

// Validation types
export interface ValidationRule {
  required?: boolean;
  minLength?: number;
  maxLength?: number;
  pattern?: RegExp;
  custom?: (value: any) => boolean | string;
}

export interface ValidationResult {
  isValid: boolean;
  errors: string[];
}

export type ValidationRules<T> = {
  [K in keyof T]?: ValidationRule[];
};

// Export all types
export * from './tauri';
